﻿using System.Linq;
using ShoppingCart.DataAccess.Abstract;
using ShoppingCart.Domain.Entities;
using System.Data.Linq;
using System.Collections.Generic;

namespace ShoppingCart.DataAccess.Concrete {

    public class ProductRepository : IProductRepository {
        ShoppingCart.DataAccess.DataProvicer.ShoppingCartDataContext context;
        public ProductRepository()
        {

            context = new ShoppingCart.DataAccess.DataProvicer.ShoppingCartDataContext();
        }

        private Product Convert(ShoppingCart.DataAccess.DataProvicer.Product p)
        {
            if (p == null)
            {
                return null;
            }
            Product product = new Product
            {
                Category = p.Category,
                Description = p.Description,
                Name = p.Name,
                Price = p.Price,
                ProductID = p.ProductID
            };
            return product;
        }

        private ShoppingCart.DataAccess.DataProvicer.Product Convert(Product p)
        {
            if (p == null)
            {
                return null;
            }
            ShoppingCart.DataAccess.DataProvicer.Product product = new ShoppingCart.DataAccess.DataProvicer.Product
            {
                Category = p.Category,
                Description = p.Description,
                Name = p.Name,
                Price = p.Price,
                ProductID = p.ProductID
            };
            return product;
        }


        private IQueryable<Product> Convert(IQueryable<ShoppingCart.DataAccess.DataProvicer.Product> p)
        {
            List<Product> querayable = new List<Product>();
            foreach (ShoppingCart.DataAccess.DataProvicer.Product product in p)
            {
                querayable.Add(Convert(product));
            }
            return querayable.AsQueryable();
        }

        public IQueryable<Product> Products
        {
            get
            {
                return Convert(context.Products.AsQueryable());
            }
        }

        public void SaveProduct(Product product) {
            if (product.ProductID == 0)
            {
                ShoppingCart.DataAccess.DataProvicer.Product p = Convert(product);
                context.Products.InsertOnSubmit(p);
            }
            else
            {
                ShoppingCart.DataAccess.DataProvicer.Product prod = context.Products.Single(p => p.ProductID == product.ProductID);
                prod.Category = product.Category;
                prod.Description = product.Description;
                prod.Name = product.Name;
                prod.Price = product.Price;
            }
            context.SubmitChanges();
        }

        public void DeleteProduct(Product product) {
            ShoppingCart.DataAccess.DataProvicer.Product productToDelete = context.Products.Where(p => p.ProductID == product.ProductID).FirstOrDefault();
           if (productToDelete != null)
           {
               context.Products.DeleteOnSubmit(productToDelete);
               context.SubmitChanges();
           }
            
        }
    }
}